import Vue from 'vue'
import VueRouter from 'vue-router'
import config from './config'
import store from '@/stores'

Vue.use(VueRouter);

let router = new VueRouter(config);

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

router.beforeEach(function(to, from, next) {
    console.log("to", to);
    console.log("from", from);
    console.log("====>" + store.state.userStore);
    console.log("====>" + store.state.userStore.user);
    if (to.meta.auth) {
      if (store.state.userStore) {
        next({ name: "Auth" });
      } else if (store.state.userStore.user) {
        next(); //允许进入
      } else {
        next({ name: "Login" });
      }
    } else {
      next();
    }
  });

export default router